package com.ruoyi.system.service.vantservice.customerervice.impl;

import com.ruoyi.system.domain.customer.CustomerAddr;
import com.ruoyi.system.domain.customer.CustomerCoupon;
import com.ruoyi.system.domain.supe.PlatformCoupon;
import com.ruoyi.system.domain.zhuangxiumain.shopMain.PreferentialCard;
import com.ruoyi.system.mapper.vant.customer.ApiCustomerMapper;
import com.ruoyi.system.service.vantservice.customerervice.ApiCustomerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ApiCustomerServiceImpl implements ApiCustomerService {

    @Autowired
    private ApiCustomerMapper customerMapper;


    /**
     * 查询用户地址列表
     * @return
     */
    public List<CustomerAddr> selectCustomerAddressList(Long customerId){
        return customerMapper.selectCustomerAddressList(customerId);
    }

    /**
     * 添加地址信息
     * @param customerAddr
     * @return
     */
    public int insertCustomerAddr(CustomerAddr customerAddr){
        if(customerAddr.getIsDefault().equals("1") ||customerAddr.getIsDefault() == "1"){
            // 先查询默认的地址信息，改为非默认地址
            CustomerAddr  customerA = customerMapper.selectCustomerAddrByDefault(customerAddr);
            if(customerA != null){
                CustomerAddr customerAd = new CustomerAddr();
                customerAd.setCustomerAddrId(customerA.getCustomerAddrId());
                customerAd.setIsDefault("0");
                this.updateCustomerAddr(customerAd);
            }
        }
        return customerMapper.insertCustomerAddr(customerAddr);
    }

    /**
     * 查询用户地址
     *
     * @param customerAddrId 用户地址ID
     * @return 用户地址
     */
    public CustomerAddr selectCustomerAddrById(Long customerAddrId){
        return customerMapper.selectCustomerAddrById(customerAddrId);
    }

    /**
     * 根据用户ID查询用户默认的地址信息
     * @param customerId
     * @return
     */
    public CustomerAddr selectCustomerAddrByCustomerId(Long customerId){
        CustomerAddr customerAddr = new CustomerAddr();
        customerAddr.setCustomerId(customerId);
        customerAddr.setIsDefault("1");
        return customerMapper.selectCustomerAddrByCustomerId(customerAddr);
    }

    /**
     * 修改用户地址
     *
     * @param customerAddr 用户地址
     * @return 结果
     */
    public int updateCustomerAddr(CustomerAddr customerAddr,Long customerId){
        // 查询原本的默认地址
        CustomerAddr customerAddres =  this.selectCustomerAddrByCustomerId(customerId);
        if(customerAddres == null){ // 如果为空，则直接修改
            return customerMapper.updateCustomerAddr(customerAddr);
        }else{ // 如果不是空,把原默认地址修改为不是默认地址
            customerAddres.setIsDefault("0");
            customerMapper.updateCustomerAddr(customerAddres);
        }
        return customerMapper.updateCustomerAddr(customerAddr);
    }

    /**
     * 修改用户地址
     *
     * @param customerAddr 用户地址
     * @return 结果
     */
    public int updateCustomerAddr(CustomerAddr customerAddr){
        return customerMapper.updateCustomerAddr(customerAddr);
    }

    /**
     * 切换默认地址信息
     * @param addressId
     * @return
     */
    public int updateCustomerAddrDefault(Long addressId,Long customerId){
        // 查询原本的默认地址
       CustomerAddr customerAddr =  this.selectCustomerAddrByCustomerId(customerId);
       if(customerAddr == null){ // 如果为空，则直接修改成默认地址
           customerAddr = new CustomerAddr();
           customerAddr.setCustomerAddrId(addressId);
           customerAddr.setIsDefault("1");
           return this.updateCustomerAddr(customerAddr);
       }else{ // 如果不是空,把原默认地址修改为不是默认地址
           customerAddr.setIsDefault("0");
           this.updateCustomerAddr(customerAddr);
       }
        CustomerAddr addr = new CustomerAddr();
        addr.setCustomerAddrId(addressId);
        addr.setIsDefault("1");
        return this.updateCustomerAddr(addr);
    }

    /**
     * 删除用户地址
     *
     * @param customerAddrId 用户地址ID
     * @return 结果
     */
    public int deleteCustomerAddrById(Long customerAddrId){
        return customerMapper.deleteCustomerAddrById(customerAddrId);
    }

    /**
     * 查询客户可用店铺优惠券
     * @param customerId
     * @param supplierId
     * @return
     */
    public List<CustomerCoupon> selectCustomerCouponList(Integer customerId,Integer supplierId){
        CustomerCoupon customerCoupon = new CustomerCoupon();
        customerCoupon.setCustomerId(customerId);
        customerCoupon.setSupplierId(supplierId);
        return customerMapper.selectCustomerCouponList(customerCoupon);
    }

    /**
     * 我的：查询客户可用的优惠券
     * @param customerId
     * @return
     */
    public List<CustomerCoupon> selectCustomerCoupon(Long customerId){
        return customerMapper.selectCustomerCoupon(customerId);
    }

    /**
     * 领取店铺优惠券
     * @param preferentialCard
     * @param customerId
     * @return
     */
    public int installCustomerCode(PreferentialCard preferentialCard, Long customerId){
        CustomerCoupon customerCoupon = new CustomerCoupon();
        customerCoupon.setSupplierId(preferentialCard.getSupplierId());
        customerCoupon.setCustomerId(customerId.intValue());
        customerCoupon.setProductId(preferentialCard.getProductId());
        customerCoupon.setCouponName(preferentialCard.getPreferentialName());
        customerCoupon.setCouponTime(preferentialCard.getPreferentialTime());
        customerCoupon.setCouponPrice(preferentialCard.getPreferentialPrice());
        customerCoupon.setCouponPriceMost(preferentialCard.getPreferentialPriceMost());
        int count = customerMapper.selectCustomerCouponByName(customerCoupon);
        if(count > 0){
            return 0;
        }else{
            return customerMapper.installCustomerCode(customerCoupon);
        }
    }

    /**
     * 领取平台优惠券
     * @param platformCoupon
     * @param customerId
     * @return
     */
    public int addPlatformCouponCustomerCode(PlatformCoupon platformCoupon, Long customerId){
        CustomerCoupon customerCoupon = new CustomerCoupon();
        customerCoupon.setSupplierId(platformCoupon.getSupplierId());
        customerCoupon.setCustomerId(customerId.intValue());
        customerCoupon.setCouponName(platformCoupon.getPlatformName());
        customerCoupon.setCouponTime(platformCoupon.getPlatformTimeEnd());
        customerCoupon.setCouponPrice(platformCoupon.getPlatformPrice());
        customerCoupon.setCouponPriceMost(platformCoupon.getPlatformPriceMost());
        customerCoupon.setModifiedTime(platformCoupon.getPlatformTimeEnd());
        int count = customerMapper.selectCustomerCouponByName(customerCoupon);
        if(count > 0){
            return 0;
        }else{
            return customerMapper.installCustomerCode(customerCoupon);
        }
    }

    /**
     * 设置优惠券状态为已使用
     * @param couponId
     * @return
     */
    public int updateCustomerCouponState(Long couponId){
        return customerMapper.updateCustomerCouponState(couponId);
    }
}
